import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsOptional, IsArray, ValidateNested, IsNumber, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class ExcelColumnDto {
  @ApiProperty({
    description: '列标题',
    example: 'ID'
  })
  @IsString()
  header: string;

  @ApiProperty({
    description: '列键名',
    example: 'id'
  })
  @IsString()
  key: string;

  @ApiProperty({
    description: '列宽度',
    example: 10,
    required: false
  })
  @IsOptional()
  @IsNumber()
  @Min(1)
  width?: number;
}

export class CreateCustomExcelDto {
  @ApiProperty({
    description: '工作表名称',
    example: '用户数据',
    required: false,
    default: '自定义数据表'
  })
  @IsOptional()
  @IsString()
  sheetName?: string;

  @ApiProperty({
    description: '列定义数组',
    type: [ExcelColumnDto],
    example: [
      { header: 'ID', key: 'id', width: 10 },
      { header: '姓名', key: 'name', width: 15 },
      { header: '邮箱', key: 'email', width: 25 }
    ]
  })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ExcelColumnDto)
  columns: ExcelColumnDto[];

  @ApiProperty({
    description: '数据数组',
    example: [
      { id: 1, name: '张三', email: 'zhangsan@example.com' },
      { id: 2, name: '李四', email: 'lisi@example.com' }
    ]
  })
  @IsArray()
  data: any[];

  @ApiProperty({
    description: '文件名',
    example: 'users.xlsx',
    required: false
  })
  @IsOptional()
  @IsString()
  fileName?: string;
}

export class ExcelServiceStatusDto {
  @ApiProperty({
    description: '操作是否成功',
    example: true
  })
  success: boolean;

  @ApiProperty({
    description: '状态消息',
    example: 'Excel服务运行正常'
  })
  message: string;

  @ApiProperty({
    description: '时间戳',
    example: '2025-06-22T10:30:00.000Z'
  })
  timestamp: string;

  @ApiProperty({
    description: '服务名称',
    example: 'ExcelService'
  })
  service: string;

  @ApiProperty({
    description: '版本号',
    example: '1.0.0'
  })
  version: string;
}

export class ExcelApiEndpointDto {
  @ApiProperty({ description: '接口描述' })
  description: string;

  @ApiProperty({ description: '参数说明', required: false })
  parameters?: Record<string, string>;

  @ApiProperty({ description: '请求体说明', required: false })
  body?: Record<string, any>;

  @ApiProperty({ description: '使用示例', required: false })
  example?: any;
}

export class ExcelHelpDto {
  @ApiProperty({
    description: '操作是否成功',
    example: true
  })
  success: boolean;

  @ApiProperty({
    description: '帮助消息',
    example: 'Excel API 使用说明'
  })
  message: string;

  @ApiProperty({
    description: '可用接口列表',
    type: 'object',
    additionalProperties: {
      type: 'object'
    }
  })
  endpoints: Record<string, ExcelApiEndpointDto>;
}
